/**
 * Copyright (c) 2015 www.smarthome.com All rights reserved. <br />
 * 本软件源代码版权归xxxx公司,未经许可不得任意复制与传播.<br />
 *
 */
package com.smarthome.core.kuaidiXiyi.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShOrderLaundryFeeService;
import com.smarthome.base.service.ShOrdersService;
import com.smarthome.core.kuaidiXiyi.XiyiOrderService;
import com.smarthome.core.messageTask.MessageTaskService;
import com.smarthome.domain.bean.ShOrderLaundryFee;
import com.smarthome.domain.bean.ShOrders;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.domain.vo.boss.TaskMessageVo;

/**
 * 洗衣service层
 * @author szc
 *
 */
@Service("xiyiOrderService")
public class XiyiOrderServiceImpl implements XiyiOrderService,MessageTaskService {

    protected static Logger LOGGER;

    @Resource
    private ShOrdersService shOrdersService;
    

	@Resource
	private ShOrderLaundryFeeService shOrderLaundryFeeService;

    /**
     * 查询列表--翻页
     * @param deviceSupplier
     * @param page
     * @author shazhichao
     * @return
     */
    @Override
    public DataGrid dataGrid(ShOrders shOrders, PageHelper page) {
        return shOrdersService.queryShOrdersPage(shOrders, page);
    }

    
    /**
     * 新增数据
     */
    @Override
    public void add(ShOrders shOrders) {
    	shOrdersService.insert(shOrders);
    }

    /**
     * 通过id主键，得到对象信息
     */
    @Override
    public ShOrders queryShOrdersById(Long id) {
        return shOrdersService.selectEntryById(id);
    }

    /**
     * 修改
     */
    @Override
    public void edit(ShOrders shOrders) {
    	shOrdersService.batchUpdate(shOrders);
    }

    

   /**
    * 得到订单list   
    */
    @Override
    public List<ShOrders> queryShOrdersList(ShOrders shOrders) {
        return shOrdersService.getShOrdersList(shOrders);
    }


    /**
     * 提醒
     */
	@Override
	public TaskMessageVo getTaskMessage(Long residenceId) {//,ShOrders shOrders
		TaskMessageVo vo = new TaskMessageVo();
		vo.setMessageType(TaskMessageVo.TaskMessageType.WASHSERVICE);
		ShOrders shOrders = new ShOrders();
		shOrders.setResidenceId(residenceId);
		shOrders.setStatus(8);//代洗衣物：1待处理、5已取件、7已送洗、8待取、2完成、3关闭、4删除
		 List<ShOrders> list = shOrdersService.getShOrdersList(shOrders);
		vo.setCount(Long.valueOf(list.size()));
		vo.setParam("status=8");
		//vo.setDescript("【代洗衣提醒】洗衣面单："+shOrders.getOutBizNum());
		return vo;
	}


	@Override
	public Integer saveOrUpdateOrderFee(Long orderId,List<ShOrderLaundryFee> list) {
		int num = 0;
		if(orderId == null) {
			return -1;
		}
		ShOrderLaundryFee orderLaundryFee = new ShOrderLaundryFee();
		orderLaundryFee.setOrderId(orderId);
		List<ShOrderLaundryFee> oldOrderFeeList = shOrderLaundryFeeService.selectEntryList(orderLaundryFee);
		Date now = new Date();
		if(oldOrderFeeList == null || oldOrderFeeList.isEmpty()) {//之前没有
			if(list == null || list.isEmpty()) {//要添加的也没有
				return num;
			}else {//有要添加的，完全新增
				int length = list.size();
				for(int i = 0;i < length;i++) {
					list.get(i).setOrderId(orderId);
					list.get(i).setGmtCreate(now);
					list.get(i).setGmtModify(now);
				}
				try {
					num = shOrderLaundryFeeService.insert(list.toArray(new ShOrderLaundryFee[list.size()]));
				}catch(Exception e) {
					e.printStackTrace();
					return -1;
				}
				return num;
			}
		}else {//之前有
			//删除
			if(list == null || list.isEmpty()) {//删除全部
				try {
					num = shOrderLaundryFeeService.deleteByCondtion(orderLaundryFee);
				}catch(Exception e) {
					e.printStackTrace();
					return -1;
				}
				return num;
			}else {//对比删除
				int length = oldOrderFeeList.size();
				List<Long> delList = new ArrayList<Long>();
				for(int i = 0;i < length;i++) {
					if(!list.contains(oldOrderFeeList.get(i))) {
						delList.add(oldOrderFeeList.get(i).getId());
					}
				}
				try {
					num = shOrderLaundryFeeService.deleteByIds(delList.toArray(new Long[delList.size()]));
				}catch(Exception e) {
					e.printStackTrace();
					return -1;
				}
				//添加或修改
				length = list.size();
				List<ShOrderLaundryFee> newOrderFeeList = new ArrayList<ShOrderLaundryFee>();
				try {
					for(int i = 0;i < length;i++) {
						list.get(i).setOrderId(orderId);
						if(list.get(i).getId() != null) {//修改
							list.get(i).setGmtModify(now);
							num += shOrderLaundryFeeService.updateById(list.get(i));
						}else {//新增
							list.get(i).setGmtCreate(now);
							newOrderFeeList.add(list.get(i));
						}
					}
				}catch(Exception e) {
					e.printStackTrace();
					return -1;
				}
				try {
					num += shOrderLaundryFeeService.insert(newOrderFeeList.toArray(new ShOrderLaundryFee[newOrderFeeList.size()]));
				}catch(Exception e) {
					e.printStackTrace();
					return -1;
				}
				return num;
			}
		}
		
	}


	@Override
	public List<ShOrderLaundryFee> getOrderFeeList(Long orderId) {
		ShOrderLaundryFee orderLaundryFee = new ShOrderLaundryFee();
		orderLaundryFee.setOrderId(orderId);
		return shOrderLaundryFeeService.selectEntryList(orderLaundryFee);
	}
	
	
}
